/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.exam;

import java.security.MessageDigest;
import java.util.Random;

/**
 * <p>Project: javaseapp - Util
 * <p>Powered by webrx On 2021-08-07 11:47:15
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Util {
    public String getPassword(int... n) {
        int num = n.length == 0 ? 6 : n[0];
        String str = "0123456789_-=+'\",`~!@#$%^&*()abcdefghijklmnolpqrstuvwxyzABCDEFGHIJKLMNOLPQRSTUVWXYZ";
        StringBuilder sbu = new StringBuilder(6);
        Random rand = new Random();
        for (int i = 0; i < num; i++) {
            sbu.append(str.charAt(rand.nextInt(str.length())));
        }
        return sbu.toString();
    }

    public String checkcode() {
        return checkcode(4);
    }

    public String checkcode(int n) {
        String str = "0123456789abcdefghijklmnolpqrstuvwxyzABCDEFGHIJKLMNOLPQRSTUVWXYZ";
        StringBuilder sbu = new StringBuilder(6);
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            sbu.append(str.charAt(rand.nextInt(str.length())));
        }
        return sbu.toString();
    }

    //md5(32) sha1(40)
    public String getMdString(String password, String algorithm) {
        StringBuilder sbu = new StringBuilder();
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        md.update(password.getBytes());
        for (byte b : md.digest()) {
            sbu.append(String.format("%02x", b));
        }
        return sbu.toString();
    }

}
